﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Db2Model
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.button2_Click(null, null);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!System.IO.Directory.Exists("Model"))
            {
                System.IO.Directory.CreateDirectory("Model");
            }
            if (!System.IO.Directory.Exists("DAL"))
            {
                System.IO.Directory.CreateDirectory("DAL");
            }
            foreach (CheckBox cb in this.flowLayoutPanel1.Controls)
            {
                if (cb.Checked)
                {
                    string code = null;
                    if (ckbModel.Checked)
                    {
                        ModelInfo mi = new ModelInfo();
                        mi.ConnectionString = txtConnectionString.Text;
                        mi.DbDatabase = txtDb.Text;
                        mi.TableName = cb.Text;

                        code = mi.TransformText();
                        System.IO.File.WriteAllText(Path.Combine("Model", mi.TableName + "Info.cs"), code);
                    }

                    if (ckbDAL.Checked)
                    {
                        DAL dal = new DAL();
                        dal.ConnectionString = txtConnectionString.Text;
                        dal.DbDatabase = txtDb.Text;
                        dal.TableName = cb.Text;

                        code = dal.TransformText();
                        System.IO.File.WriteAllText(Path.Combine("DAL", dal.TableName + ".cs"), code);
                    }

                }
            }
            MessageBox.Show("生成完成");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                var tables = DbHelper.GetDbTables(txtConnectionString.Text,
                              txtDb.Text, txtSearch.Text);
                this.flowLayoutPanel1.Controls.Clear();
                if (tables != null)
                {
                    foreach (var table in tables)
                    {
                        CheckBox cb = new CheckBox();
                        cb.Text = table.TableName;
                        if(string.IsNullOrEmpty(table.TableDesc))
                            toolTip1.SetToolTip(cb, table.TableName);
                        else
                            toolTip1.SetToolTip(cb, string.Format("{0}({1})", table.TableName, table.TableDesc) );
                        this.flowLayoutPanel1.Controls.Add(cb);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (CheckBox cb in this.flowLayoutPanel1.Controls)
            {
                cb.Checked ^= true;
            }
        }

        private void btnOpenDir_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Application.StartupPath);
        }
    }
}
